package com.qf.controller;

import com.alibaba.druid.util.StringUtils;
import com.qf.pojo.DtsUserAccount;
import com.qf.service.DtsUserAccountService;
import com.qf.service.DtsUserService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.StringReader;
import java.util.Map;

/**admin/user/list?page=1&limit=20&sort=add_time&order=desc
 *   会员管理
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/10/15 16:55
 */
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    DtsUserService userService;
    @Autowired
    DtsUserAccountService userAccountService;

    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",required = false, defaultValue = "20")Integer limit,
                       @RequestParam(value = "sort",required = false, defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",required = false, defaultValue = "desc")String order,
                       String username, String mobile){

        Map<String,Object> map = userService.findByPage(page,limit,sort,order,username,mobile);

        return ResponseUtil.ok(map);
    }

    @PostMapping("/approveAgency")
    public Object approveAgency(@RequestBody Map<String, String> map){
//        {userId: 1, settlementRate: "123"}
        int userId = Integer.parseInt(String.valueOf(map.get("userId")));
        int settlementRate = 0 ;
        if (!StringUtils.isEmpty(map.get("settlementRate"))){
             settlementRate =  Integer.parseInt((map.get("settlementRate")));
        }

       userAccountService.updateRateByUserId(userId,settlementRate);

        return ResponseUtil.ok();
    }

    @RequestMapping("/detailApprove")
    public Object detailApprove(Integer id){
        DtsUserAccount userAccount = userAccountService.selectById(id);
        return ResponseUtil.ok(userAccount);
    }
}
